//
//  AUDViewController.swift
//  PassValueDemo
//
//  Created by MAC on 2020/8/26.
//  Copyright © 2020 MAC. All rights reserved.
//

import UIKit

class AUDViewController: UIViewController {

    //声明变量名，并初始化
    var textF = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //传值按钮
        let ymBtn_01 = UIButton()
        ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
        ymBtn_01.setTitle("跳转到上级页面，并传回值", for: .normal)
        ymBtn_01.backgroundColor = .brown
        ymBtn_01.tag = 10001
        ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
        view.addSubview(ymBtn_01)
        
        //传值的输入框
        textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
        textF.attributedPlaceholder = NSAttributedString(string: "请输入要传的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.5)])
        textF.borderStyle = .line
        textF.textColor = .white
        textF.backgroundColor = UIColor.init(red: 0.283, green: 0.290, blue: 0.293, alpha: 1)
        view.addSubview(textF)
        
        let textLabel_1 = UILabel()
        textLabel_1.font = .systemFont(ofSize: 20)
        textLabel_1.frame = CGRect(x: 50, y: (textF.frame.origin.y+textF.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
        textLabel_1.text = "请先上面的输入框中填写要传回的值"
        textLabel_1.numberOfLines = 0
        textLabel_1.lineBreakMode = .byWordWrapping
        textLabel_1.backgroundColor = UIColor.init(red: 0.238, green: 0.240, blue: 0.242, alpha: 0.5)
        textLabel_1.textColor = .yellow
        textLabel_1.font = .systemFont(ofSize: 15)
        textLabel_1.textAlignment = .center
        view.addSubview(textLabel_1)
        
        
    }
    
    //实现传值按钮的监听方法
    @objc func tiaozhuanyemianclick(sender:UIButton){
        //使用UserDefaults把需要传的值，先存到本地沙盒中，存储是根据key来存储的，一个key对应一个值（可以是可变数组，或可变字典）
        /*
        Swift之UserDefaults数据存储 https://www.jianshu.com/p/99660cadfe16
        1、存储各个类型的数据
        2、获取各个类型的存储数据
        */
        //声明数组变量名，并填入数据
        let array =  ["宝马", "奔驰", "奥迪", "兰博基尼", "凯迪拉克", "法拉利"]
        //通过key来存储数组
        UserDefaults().set(array, forKey: "userNameArray")
        //声明字典变量名，并填入数据
        let dic =  ["BM":"宝马", "BC":"奔驰", "AD":"奥迪", "LBJN":"兰博基尼", "KDLK":"凯迪拉克", "FLL":"法拉利"]
        //通过key来存储字典
        UserDefaults().set(dic, forKey: "userNameDictinary")
        //通过key来存储字符串
        UserDefaults().set(textF.text!, forKey: "userName")
        //返回上级页面
        self.navigationController?.popViewController(animated: true)
    }
}
